// pages/company/company.js
const app = getApp()
const { request } = require("../../utils/request")

Page({
  data: {
    companies: [], // 公司列表
    showModal: false,
    isEdit: false,
    currentCompany: { id: "", name: "", discount: "" },
  },

  onLoad() {
    this.loadCompanies()
  },

  // 加载公司列表
  async loadCompanies() {
    wx.showLoading({ title: "加载中..." })
    request({
      url: `${app.globalData.baseUrl}/sadmin/company_list`,
      method: "GET",
    })
      .then((res) => {
        if (res.statusCode === 200) {
          this.setData({
            companies: res.data.data,
          })
        }
      })
      .catch(() => wx.showToast({ title: "加载失败", icon: "none" }))
      .finally(() => wx.hideLoading())
  },

  // 显示新增弹窗
  showAddModal() {
    this.setData({
      showModal: true,
      isEdit: false,
      currentCompany: { name: "", discount: "" },
    })
  },

  // 编辑公司
  editCompany(e) {
    const id = e.currentTarget.dataset.id
    const company = this.data.companies.find((item) => item.id === id)
    this.setData({
      showModal: true,
      isEdit: true,
      currentCompany: { ...company },
    })
  },

  updateName(e) {
    this.setData({
      "currentCompany.name": e.detail.value,
    })
  },

  updateDiscount(e) {
    this.setData({
      "currentCompany.discount": e.detail.value,
    })
  },

  checkCurrentCompany() {
    if (this.data.currentCompany.name == "") {
      wx.showToast({ title: "请输入有效公司名", icon: "none" })
      return false
    }
    if (!/^-?\d+(\.\d+)?$/.test(this.data.currentCompany.discount)) {
      wx.showToast({ title: "请输入有效折扣", icon: "none" })
      return false
    }
    return true
  },

  // 提交表单
  submitForm() {
    if (!this.checkCurrentCompany()) {
      return
    }
    if (this.data.currentCompany.discount < 0 || this.data.currentCompany.discount > 1) {
      wx.showToast({ title: "请输入有效数字", icon: "none" })
      return
    }

    if (this.data.isEdit) {
      request({
        url: `${app.globalData.baseUrl}/sadmin/company_list`,
        method: "PUT",
        data: this.data.currentCompany,
      })
        .then((res) => {
          if (res.statusCode === 200) {
            this.setData({
              companies: res.data.data,
            })
          }
        })
        .catch(() => wx.showToast({ title: "编辑失败", icon: "none" }))
    } else {
      request({
        url: `${app.globalData.baseUrl}/sadmin/company_list`,
        method: "POST",
        data: this.data.currentCompany,
      })
        .then((res) => {
          if (res.statusCode === 200) {
            this.setData({
              companies: res.data.data,
            })
          }
        })
        .catch(() => wx.showToast({ title: "新增失败", icon: "none" }))
    }
    this.hideModal()
  },

  // 删除公司
  deleteCompany(e) {
    const id = e.currentTarget.dataset.id
    wx.showModal({
      title: "确认删除",
      content: "确定要删除该公司吗？",
      success: (res) => {
        if (res.confirm) {
          request({
            url: `${app.globalData.baseUrl}/sadmin/company_list?id=${id}`,
            method: "DELETE",
          })
            .then((res) => {
              if (res.statusCode === 200) {
                this.setData({
                  companies: res.data.data,
                })
              }
            })
            .catch(() => wx.showToast({ title: "删除失败", icon: "none" }))
        }
      },
    })
  },

  hideModal() {
    this.setData({ showModal: false })
  },
})
